package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.Record;
import com.trace.app.entity.Record;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.User;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.entity.vo.RecordVo;
import com.trace.app.mapper.RecordMapper;
import com.trace.app.service.RecordService;
import com.trace.app.service.UserService;
import com.trace.app.util.AppHttpCodeEnum;
import com.trace.app.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Record)表服务实现类
 *
 * @author lionkliu
 * @since 2024-03-17 15:55:23
 */
@Service("recordService")
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();
        Page<Record> page = new Page<>(pageNum, pageSize);
        Page<Record> recordPage = page(page, queryWrapper);
        PageVo<Record> pageVo = new PageVo<>();
        pageVo.setResult(recordPage.getRecords());
        pageVo.setTotal(recordPage.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Record record) {
        return save(record) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Record record) {
        UpdateWrapper<Record> updateWrapper = new UpdateWrapper();
        // TODO：
        return update(updateWrapper) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult getRecordsByProId(Integer proId) {
        LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Record::getProId, proId);
        List<Record> records = list(queryWrapper);
        List<RecordVo> recordVoList = records.stream().map(record -> {
            RecordVo recordVo = BeanCopyUtils.copyBean(record, RecordVo.class);
            User user = userService.getById(record.getUserId());
            if (!Objects.isNull(user)) {
                recordVo.setUser(user);
            }
            return recordVo;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(recordVoList);
    }

}

